package com.gxa.jd.controller;

import com.gxa.entity.Result;
import com.gxa.entity.StatusCode;
import com.gxa.exception.CustomerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
 */
@ControllerAdvice
@Slf4j
public class BaseExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(Exception e) {

        log.error(e.getMessage());

        if(e instanceof CustomerException){
            CustomerException customerException= (CustomerException) e;
            return new Result(customerException.getMessageCode().getCode(),customerException.getMessageCode().getMessage());
        }

        return new Result(StatusCode.ERROR, "执行出错");
    }
}
